//
//  MyOrderTableViewCell.m
//  X-Touch2
//
//  Created by shengchao yang on 12-3-28.
//  Copyright (c) 2012年 foxhis. All rights reserved.
//

#import "MyOrderTableViewCell.h"
#import "MyOrderTableViewCellController.h"
#import "Common.h"
#import "Singleton.h"
#import "NSString+Additions.h"
#import "FileManager.h"

@interface MyOrderTableViewCell(Privete)
    -(void)minusNumber;
    -(void)addNumber;
    -(void)deleteMyOrderDish;
@end

@implementation MyOrderTableViewCell

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
         MyOrderTableViewCellController *cellController = [[[MyOrderTableViewCellController alloc] initWithNibName:@"MyOrderTableViewCellController" bundle:nil] autorelease];
        [self.contentView addSubview:cellController.view];
        
        [((UIButton *)[self viewWithTag:TagOfUIButton]) addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchDown];
        [((UIButton *)[self viewWithTag:TagOfUIButton+1]) addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchDown];
        [((UIButton *)[self viewWithTag:TagOfUIButton+2]) addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchDown];
        [((UIButton *)[self viewWithTag:TagOfUIButton+3]) addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchDown];
        
        lblIndex = (UILabel *)[self viewWithTag:TagOfUILabel];
        lblPluName = (UILabel *)[self viewWithTag:TagOfUILabel+1];
        lblPluCook = (UILabel *)[self viewWithTag:TagOfUILabel+2];
        lblPluPrice = (UILabel *)[self viewWithTag:TagOfUILabel+3];
        lblPluUnit = (UILabel *)[self viewWithTag:TagOfUILabel+4];
        lblPluNumber = (UILabel *)[self viewWithTag:TagOfUILabel+5];
        lblPluTotal = (UILabel *)[self viewWithTag:TagOfUILabel+6];
    }
    return self;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated{
    [super setSelected:selected animated:animated];
    UIImageView *imageView = (UIImageView *)[self viewWithTag:TagOfUIImageView];
    if (selected) {
        imageView.hidden = NO;
        AppSession.currentMyOrderDish = myOrderDish;
    }else {
        imageView.hidden = YES;
    }
}

-(void) setData:(id)data index:(NSInteger)index{
    [((UIButton *)[self viewWithTag:TagOfUIButton+3]) setTitle:TranslateStr(@"套餐") forState:UIControlStateNormal];
    myOrderDish = (MyOrderDishes *)data;
    lblIndex.text = [NSString stringWithFormat:@"%d",index];
    lblPluName.text = JsonField(myOrderDish.modDescripts);
    lblPluCook.text = myOrderDish.modCook;
    lblPluPrice.text = myOrderDish.modPrice;
    lblPluUnit.text = JsonField(myOrderDish.modUnits);
    lblPluNumber.text = myOrderDish.modNumber;
    lblPluTotal.text = [NSString stringWithFormat:@"%0.2f",[myOrderDish.modNumber floatValue]*[myOrderDish.modPrice floatValue]];
    //控制套餐按钮是否显示
    if ([myOrderDish.ppType isEqualToString:@"3"]) {
        [((UIButton *)[self viewWithTag:TagOfUIButton+3]) setHidden:NO];
        
    }else {
        [((UIButton *)[self viewWithTag:TagOfUIButton+3]) setHidden:YES];
    }
}


- (IBAction)buttonClicked:(id)sender{
    if (AppSession.isAnimating) {
        MyNSLog(@"上一个动画还未完成...");
        return;
    }
    [AppSession playMusic:VoiceFile(@"clicked.caf")];
    if ([AppSession.currentOrderMenu.momSendErr isEqualToString:@"1"]) {
        postN(@"SysMsg_RootView_AlertErrMsg");
        return;
    }
    
    NSInteger bid = [sender tag];
    if (bid == TagOfUIButton) {
        MyNSLog(@"减去1...");        
        NSInteger numBeforeDot = [myOrderDish.modNumber intValue];//整数
        if(numBeforeDot == 0){
            alertType = 1;//减1
            UIAlertView *alert =[[ UIAlertView alloc] initWithTitle:TranslateStr(@"提示")
                                                            message:TranslateStr(@"数量已经为0了")
                                                           delegate:self 
                                                  cancelButtonTitle:TranslateStr(@"取消") 
                                                  otherButtonTitles:TranslateStr(@"删除"),/*TranslateStr(@"继续减1"),*/ nil];
            [alert show];
            [alert release];
        }else {
            [self minusNumber];
        }      
    }else if(bid == TagOfUIButton + 1){
        MyNSLog(@"加1...");
        [self addNumber];
    }else if(bid == TagOfUIButton + 2){
        MyNSLog(@"删除...");
        alertType = 2;//删除
        UIAlertView *alert =[[ UIAlertView alloc] initWithTitle:TranslateStr(@"提示") 
                                                        message:TranslateStr(@"删除确认")
                                                       delegate:self 
                                              cancelButtonTitle:TranslateStr(@"取消") 
                                              otherButtonTitles:TranslateStr(@"确定"), nil];
        [alert show];
        [alert release];
    }else if(bid == TagOfUIButton + 3){
        MyNSLog(@"弹出套餐明细");
        AppSession.currentMyOrderDish = myOrderDish;
        [AppSession initMyOrderPluPkgs:myOrderDish.ppCode];//查询套餐明细
        postN(@"SysMsg_RootView_PopOutPluPackage");
    }
}
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    if (alertType == 1) {
        if (buttonIndex==alertView.cancelButtonIndex) {
            MyNSLog(@"点击了取消按钮");
        }else if(buttonIndex == 1){
            [self deleteMyOrderDish];
        }/*else {
            [self minusNumber];
        }*/
    }else if(alertType == 2) {
        if (buttonIndex==alertView.cancelButtonIndex) {
            MyNSLog(@"点击了取消按钮");
        }else{
            [self deleteMyOrderDish];
        }
    }
}
-(void)minusNumber{
    NSInteger numBeforeDot = [myOrderDish.modNumber intValue];//整数
    NSString * newNumberString = [myOrderDish.modNumber replaceFrom:[NSString stringWithFormat:@"%d.",numBeforeDot] To:[NSString stringWithFormat:@"%d.",numBeforeDot-1]];//计算出新的数量
    [myOrderDish updateNumber:newNumberString];
    postN(@"SysMsg_MyOrderTableModeView_refreshMyOrderTable");//刷新界面
}
-(void)addNumber{
    
    NSInteger numBeforeDot = [myOrderDish.modNumber intValue];//整数
    NSString * newNumberString = [myOrderDish.modNumber replaceFrom:[NSString stringWithFormat:@"%d.",numBeforeDot] To:[NSString stringWithFormat:@"%d.",numBeforeDot+1]];//计算出新的数量
    [myOrderDish updateNumber:newNumberString]; 
    postN(@"SysMsg_MyOrderTableModeView_refreshMyOrderTable");//刷新界面
}
-(void)deleteMyOrderDish{
    NSDictionary *infos = [NSDictionary dictionaryWithObjectsAndKeys:myOrderDish.ppCode, @"ppCode", 
                           myOrderDish.modPrice,@"modPrice",
                           myOrderDish.modTime,@"modTime",
                           nil];
    postNWithInfos(@"SysMsg_MyOrderTableModeView_deleteMyOrderDish", nil, infos);
}
-(void) dealloc{
    MyNSLog(@"MyOrderTableViewCell dealloc index=%d", self.tag - TagOfUITableViewCell);
    [super dealloc];
}
@end
